// CDlgSysHardCfg.cpp : implementation file
//

#include "stdafx.h"
#include "Public.h"
#include "HubImage.h"
#include "DlgSysHardCfg.h"
#include "MoreSetDlg.h"
#include "DlgSystemParam.h"

// CDlgSysHardCfg dialog
#define Timer_DlgSysHardCfg     0x1238

IMPLEMENT_DYNAMIC(CDlgSysHardCfg, CMyDlg)

CDlgSysHardCfg::CDlgSysHardCfg(CWnd* pParent /*=NULL*/)
	: CMyDlg(CDlgSysHardCfg::IDD, pParent)
{
	memcpy((void*)&m_xCapDevDesc,(void*)&g_xCapDevDesc,sizeof(zkCapDevDesc));
	memcpy((void*)&m_xLPCCom,(void*)&g_xPLCCom,sizeof(zkComCfg));
	memcpy((void*)&m_xRayCom,(void*)&g_xRayCom,sizeof(zkComCfg));
}
CDlgSysHardCfg::~CDlgSysHardCfg()
{
}

void CDlgSysHardCfg::OnDestroy()
{
	KillTimer(Timer_DlgSysHardCfg);

	CMyDlg::OnDestroy();

	// TODO: Add your message handler code here
}


void CDlgSysHardCfg::DoDataExchange(CDataExchange* pDX)
{
	CMyDlg::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_COMBO_MOVECTRL_NAME, m_cbPLCCtrlName);
	DDX_Control(pDX, IDC_COMBO_XRAY_NAME, m_cbRayCtrlName);
	DDX_Control(pDX, IDC_COMBO_CapDev_NAME, m_cbDeviceName);
}


BEGIN_MESSAGE_MAP(CDlgSysHardCfg, CMyDlg)
	ON_BN_CLICKED(IDC_BUTTON_MORE_SET, &CDlgSysHardCfg::OnBnClickedButtonMoreSet)
	ON_CBN_SELCHANGE(IDC_COMBO_MOVECTRL_NAME, &CDlgSysHardCfg::OnCbnSelchangeComboMovectrlName)
	ON_EN_CHANGE(IDC_EDIT_MOVECTRL_MACHINE_CODE, &CDlgSysHardCfg::OnEnChangeEditMovectrlMachineCode)
	ON_EN_CHANGE(IDC_EDIT_MOVECTRL_REGISTER_CODE, &CDlgSysHardCfg::OnEnChangeEditMovectrlRegisterCode)
	ON_CBN_SELCHANGE(IDC_COMBO_XRAY_NAME, &CDlgSysHardCfg::OnCbnSelchangeComboXrayName)
	ON_EN_CHANGE(IDC_EDIT_XRAY_MACHINE_CODE, &CDlgSysHardCfg::OnEnChangeEditXrayMachineCode)
	ON_EN_CHANGE(IDC_EDIT_XRAY_REGISTER_CODE, &CDlgSysHardCfg::OnEnChangeEditXrayRegisterCode)
	ON_WM_TIMER()
	ON_WM_DESTROY()
	ON_BN_CLICKED(IDC_BUTTON_CAP_SETTING, &CDlgSysHardCfg::OnBnClickedButtonCapSetting)
	ON_CBN_SELCHANGE(IDC_COMBO_CapDev_NAME, &CDlgSysHardCfg::OnCbnSelchangeComboCapdevName)
	ON_BN_CLICKED(IDC_BUTTON_MOV_SETTING, &CDlgSysHardCfg::OnBnClickedButtonMovSetting)
END_MESSAGE_MAP()


// CDlgSysHardCfg message handlers


BOOL CDlgSysHardCfg::OnInitDialog()
{
	CMyDlg::OnInitDialog();
	int nRet ;

	zkPlugDescs *pPlugDescs = NULL;
 	if( DD_ERR_SUCCESS == g_PLCCtrl.GetPlugList(&pPlugDescs) && NULL != pPlugDescs)
 	{
		if( pPlugDescs->nPlugDescs > 0 )
		{
 			for(int i=0;i<pPlugDescs->nPlugDescs;i++)
 			{
 				m_cbPLCCtrlName.AddString( pPlugDescs->arPlugDescs[i]);
 			}
			m_cbPLCCtrlName.SetCurSel(0);
			m_cbPLCCtrlName.SelectString(-1,g_SysConfigParam.szPLCPlugName );

			OnCbnSelchangeComboMovectrlName();
		}
 	}

	pPlugDescs = NULL;
	if( DD_ERR_SUCCESS == g_RayCtrl.GetPlugList(&pPlugDescs) && NULL != pPlugDescs)
	{
		if( pPlugDescs->nPlugDescs > 0 )
		{
			for(int i=0;i<pPlugDescs->nPlugDescs;i++)
			{
				m_cbRayCtrlName.AddString( pPlugDescs->arPlugDescs[i]);
			}
			m_cbRayCtrlName.SetCurSel(0);
			m_cbRayCtrlName.SelectString(-1,g_SysConfigParam.szXRayPlugName );

			OnCbnSelchangeComboXrayName();
		}
	}

	int nCount = 0 ;
	zkCapDevDesc* pCap = NULL;
	if( DD_ERR_SUCCESS == g_HubCap.GetCaptureList(&pCap,nCount) && nCount > 0)
	{
		for(int i=0;i<nCount;i++)
		{
			m_cbDeviceName.AddString( pCap[i].cDeviceName);
		}
		m_cbDeviceName.SetCurSel(0);
		m_cbDeviceName.SelectString(-1,g_xCapDevDesc.cDeviceName);
		
		OnCbnSelchangeComboCapdevName();
	}

	return TRUE;
}

void CDlgSysHardCfg::OnTimer(UINT_PTR nIDEvent)
{



	CMyDlg::OnTimer(nIDEvent);
}

void CDlgSysHardCfg::OnApply()
{
	if( GetDlgItem(IDC_EDIT_MOVECTRL_REGISTER_CODE)->IsWindowEnabled() )
	{
		zkPlugParam xParam;
		xParam.dwType   = ePlug_Parma_SetRegisterInfo;
		xParam.bAutoDel = FALSE;
		xParam.nParamSize = sizeof(zkPlugRegParam);
		zkPlugRegParam xRegParam;
		GetDlgItemText(IDC_EDIT_MOVECTRL_REGISTER_CODE, xRegParam.szRegisterCode,200 );
		xParam.pParam = &xRegParam;
		g_PLCCtrl.OpParam( (void*)&xParam );
	}

	if( GetDlgItem(IDC_EDIT_XRAY_REGISTER_CODE)->IsWindowEnabled() )
	{
		zkPlugParam xParam;
		xParam.dwType   = ePlug_Parma_SetRegisterInfo;
		xParam.bAutoDel = FALSE;
		xParam.nParamSize = sizeof(zkPlugRegParam);
		zkPlugRegParam xRegParam;
		GetDlgItemText(IDC_EDIT_XRAY_REGISTER_CODE, xRegParam.szRegisterCode,200 );
		xParam.pParam = &xRegParam;
		g_RayCtrl.OpParam( (void*)&xParam );
	}
	if( GetDlgItem(IDC_EDIT_CapDev_REGISTER_CODE)->IsWindowEnabled() )
	{
		zkPlugParam xParam;
		xParam.dwType   = eCap_Parma_SetRegisterInfo;
		xParam.bAutoDel = FALSE;
		xParam.nParamSize = sizeof(zkPlugRegParam);
		zkPlugRegParam xRegParam;
		GetDlgItemText(IDC_EDIT_CapDev_REGISTER_CODE, xRegParam.szRegisterCode,200 );
		xParam.pParam = &xRegParam;
		g_HubCap.OpCaptureParam( (void*)&xParam );
	}
	if(memcmp((void*)&m_xCapDevDesc,(void*)&g_xCapDevDesc,sizeof(zkCapDevDesc)) != 0 )
	{
		zkPlugParam xParam;
		xParam.dwType   = eCap_Parma_SetCapNetInfo;
		xParam.bAutoDel = FALSE;
		xParam.nParamSize = sizeof(zkCapDevDesc);	
		xParam.pParam = &m_xCapDevDesc;
		g_HubCap.OpCaptureParam( (void*)&xParam );
		memcpy((void*)&g_xCapDevDesc,(void*)&m_xCapDevDesc,sizeof(zkCapDevDesc));
		WriteCfgFile((void*)&g_xCapDevDesc,sizeof(zkCapDevDesc),"CapDevDesc.bin");
	}
	if(memcmp((void*)&m_xLPCCom,(void*)&g_xPLCCom,sizeof(zkComCfg)) != 0 )
	{
		memcpy((void*)&g_xPLCCom,(void*)&m_xLPCCom,sizeof(zkComCfg));
		g_PLCCtrl.SetComCfg(&g_xPLCCom);
		WriteCfgFile((void*)&g_xPLCCom,sizeof(zkComCfg),"PLCCom.bin");
	}
	if(memcmp((void*)&m_xRayCom,(void*)&g_xRayCom,sizeof(zkComCfg)) != 0 )
	{
		memcpy((void*)&g_xRayCom,(void*)&m_xRayCom,sizeof(zkComCfg));
		g_RayCtrl.SetComCfg(&g_xRayCom);
		WriteCfgFile((void*)&g_xRayCom,sizeof(zkComCfg),"RayCom.bin");
	}

	WriteCfgFile((void*)&g_SysConfigParam,sizeof(g_SysConfigParam),"SysConfigParam.bin");
}

void CDlgSysHardCfg::OnCbnSelchangeComboMovectrlName()
{
	int nRet = 0;

	char sText[256] = {0};
	int nSel = m_cbPLCCtrlName.GetCurSel();
	if( nSel >= 0 )
	{
		m_cbPLCCtrlName.GetLBText( nSel,sText );
		sprintf(g_SysConfigParam.szPLCPlugName,sText);
	}

	zkPlugParam xParam;
	xParam.dwType = ePlug_Parma_GetRegisterInfo;
	nRet = g_PLCCtrl.OpParam( (void*)&xParam );
	if( DD_ERR_SUCCESS == nRet  )
	{
		zkPlugRegParam *pRegParam = (zkPlugRegParam *)xParam.pParam;
		if( NULL != pRegParam )
		{
			SetDlgItemText(IDC_EDIT_MOVECTRL_MACHINE_CODE, pRegParam->szMachineCode );
			if( pRegParam->dwReg > 0 )
			{
				SetDlgItemText(IDC_EDIT_MOVECTRL_REGISTER_CODE, pRegParam->szRegisterCode );
				GetDlgItem(IDC_EDIT_MOVECTRL_REGISTER_CODE)->EnableWindow(FALSE);
			}
			else
			{
				SetDlgItemText(IDC_EDIT_MOVECTRL_REGISTER_CODE, "" );
				GetDlgItem(IDC_EDIT_MOVECTRL_REGISTER_CODE)->EnableWindow(TRUE);
			}
		}
	}

}


void CDlgSysHardCfg::OnEnChangeEditMovectrlMachineCode()
{

}


void CDlgSysHardCfg::OnEnChangeEditMovectrlRegisterCode()
{
}


void CDlgSysHardCfg::OnCbnSelchangeComboXrayName()
{
	int nRet = 0;

	char sText[256] = {0};
	int nSel = m_cbRayCtrlName.GetCurSel();
	if( nSel >= 0 )
	{
		m_cbRayCtrlName.GetLBText( nSel,sText );
	}

	if( strcmp( g_SysConfigParam.szXRayPlugName,sText ) != 0 )
	{
		nRet = g_RayCtrl.SelectPlug( sText );
		if( DD_ERR_SUCCESS != nRet  )
		{
			return;
		}

		sprintf(g_SysConfigParam.szXRayPlugName,sText);
	}

	zkPlugParam xParam;
	xParam.dwType = ePlug_Parma_GetRegisterInfo;
	nRet = g_RayCtrl.OpParam(&xParam);
	if( DD_ERR_SUCCESS == nRet  )
	{
		zkPlugRegParam *pRegParam = (zkPlugRegParam *)xParam.pParam;
		if( NULL != pRegParam )
		{
			SetDlgItemText(IDC_EDIT_XRAY_MACHINE_CODE, pRegParam->szMachineCode );
			if( pRegParam->dwReg > 0 )
			{
				SetDlgItemText(IDC_EDIT_XRAY_REGISTER_CODE, pRegParam->szRegisterCode );
				GetDlgItem(IDC_EDIT_XRAY_REGISTER_CODE)->EnableWindow(FALSE);
			}
			else
			{
				SetDlgItemText(IDC_EDIT_XRAY_REGISTER_CODE, "" );
				GetDlgItem(IDC_EDIT_XRAY_REGISTER_CODE)->EnableWindow(TRUE);
			}
		}
	}

}


void CDlgSysHardCfg::OnEnChangeEditXrayMachineCode()
{

}


void CDlgSysHardCfg::OnEnChangeEditXrayRegisterCode()
{
}

void CDlgSysHardCfg::OnCbnSelchangeComboCapdevName()
{
	int nRet =0;

	char sText[256] = {0};
	int nSel = m_cbDeviceName.GetCurSel();
	if( nSel < 0 )
	{
		return;
	}

	zkCapDevDesc *pDevDesc = NULL;

	if( strlen(g_xCapDevDesc.cDevDllName) > 0 )
	{
		pDevDesc = &g_xCapDevDesc;
	}
	else
	{
		int           nDevCount = 0;
		zkCapDevDesc *pDevDescs = NULL;

		g_HubCap.GetCaptureList(&pDevDescs,nDevCount);

		if( NULL == pDevDescs || nDevCount <= 0 || nSel >= nDevCount )
		{
			return;
		}
		
		pDevDesc = &(pDevDescs[nSel]);

		if( strcmp( g_xCapDevDesc.cDeviceName,pDevDesc->cDeviceName ) != 0 )
		{
			nRet = g_HubCap.SelectCapture( pDevDesc );
		}
	}

	zkFCaptureParam xParam;
	xParam.dwType = eCap_Parma_GetRegisterInfo;
	nRet = g_HubCap.OpCaptureParam( &xParam );
	if( DD_ERR_SUCCESS == nRet  )
	{
		zkRegisterParam *pRegParam = (zkRegisterParam *)xParam.pParam;
		if( NULL != pRegParam )
		{
			SetDlgItemText(IDC_EDIT_CapDev_MACHINE_CODE, pRegParam->szMachineCode );
			if( pRegParam->dwReg > 0 )
			{
				SetDlgItemText(IDC_EDIT_CapDev_REGISTER_CODE, pRegParam->szRegisterCode );
				GetDlgItem(IDC_EDIT_CapDev_REGISTER_CODE)->EnableWindow(FALSE);
				BOOL bShowWindow = pDevDesc->ucNet == 1 ? TRUE:FALSE;
				GetDlgItem(IDC_BUTTON_CAP_SETTING)->EnableWindow(bShowWindow);
			}
			else
			{
				SetDlgItemText(IDC_EDIT_CapDev_REGISTER_CODE, "" );
				GetDlgItem(IDC_EDIT_CapDev_REGISTER_CODE)->EnableWindow(TRUE);
			}
		}
	}
}

#include "DlgNetMoreSet.h"

void CDlgSysHardCfg::OnBnClickedButtonCapSetting()
{
	DlgNetMoreSet dlg;
	dlg.m_pCapDevDesc = &m_xCapDevDesc;
	dlg.DoModal();
}


void CDlgSysHardCfg::OnBnClickedButtonMovSetting()
{
 	CMoreSetDlg dlg;
	dlg.m_pCfg = &m_xLPCCom;
	dlg.DoModal();
}
void CDlgSysHardCfg::OnBnClickedButtonMoreSet()
{
	CMoreSetDlg dlg;
	dlg.m_pCfg = &m_xRayCom;
	dlg.DoModal();
}